<?php

/**
 * This is the model class for table "b2c_msg_records".
 *
 * The followings are the available columns in table 'b2c_msg_records':
 * @property integer $id
 * @property integer $uid
 * @property integer $type
 * @property string $title
 * @property string $description
 * @property integer $push
 * @property string $create_at
 * @property string $update_at
 */
class B2cMsgRecords extends CActiveRecord
{
    const MSG_TYPE_PURCHASE = 1;
    
    const MSG_PUSH_DISABLE = 0;
    const MSG_PUSH_ENABLE = 1;
    const MSG_PUSHING = 2;
    const MSG_PUSH_FAILED = 3;
    const MSG_PUSH_SUCCESS = 4;
    
    
    
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return B2cMsgRecords the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'b2c_msg_records';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, title, description, push', 'required'),
            array('uid, type, push', 'numerical', 'integerOnly'=>true),
            array('title', 'length', 'max'=>200),
            array('description', 'length', 'max'=>500),
            array('create_at, update_at', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, uid, type, title, description, push, create_at, update_at', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'uid' => 'Uid',
            'type' => 'Type',
            'title' => 'Title',
            'description' => 'Description',
            'push' => 'Push',
            'create_at' => 'Create At',
            'update_at' => 'Update At',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('uid',$this->uid);
        $criteria->compare('type',$this->type);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('push',$this->push);
        $criteria->compare('create_at',$this->create_at,true);
        $criteria->compare('update_at',$this->update_at,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    
    public function genPurchaseRecords($item)
    {
        $record = new B2cMsgRecords();
        $record->uid = $item["uid"];
        $record->type = B2cMsgRecords::MSG_TYPE_PURCHASE;
        $record->title = "您好，您购买的商品已成功支付";
        
        $record->description = "您已成功购买本公司产品，激活码为";
        foreach($item["items"] as $it){
            $record->description .= $it["activecode"]."，";
        }
        $record->description .= "请妥善保管。";
        $record->create_at = new CDbExpression("NOW()");
        $record->push = B2cMsgRecords::MSG_PUSH_ENABLE;
        try {
            if(false === $record->save()){
                var_dump($record->getErrors());
                throw new Exception("购买记录保存失败");
            }
        } catch (Exception $exc) {
            echo $exc->getMessage();
            return false;
        }
        $record->doPush();
        return true;
    }
    public function doPush()
    {
        if($this->push === B2cMsgRecords::MSG_PUSH_DISABLE)
            return;
        if($this->push === B2cMsgRecords::MSG_PUSH_ENABLE){
            //push消息
            //
            $push = new JPushService();
            $ret = $push->send($this->uid, $this->description, $this->id);
            if($ret->errcode != 0){
                $this->push = B2cMsgRecords::MSG_PUSH_FAILED;
            }else{
                $this->push = B2cMsgRecords::MSG_PUSHING;
            }
            
            try{
                if(false===$this->update()){
                    throw new Exception("推送标志更新失败");
                }
            } catch (Exception $ex) {
                return false;
            }
            
        }
        
    }
}